package org.jeecg.modules.crm.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.crm.entity.CrmProject;
import org.jeecg.modules.crm.service.ICrmProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 项目信息
 * @Author: jeecg-boot
 * @Date:   2023-06-03
 * @Version: V1.0
 */
@Api(tags="项目信息")
@RestController
@RequestMapping("/crm/project")
@Slf4j
public class CrmProjectController extends JeecgController<CrmProject, ICrmProjectService> {
    @Autowired
    private ICrmProjectService projectService;
    
    /**
     * 分页列表查询
     *
     * @param project
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "项目信息-分页列表查询")
    @ApiOperation(value="项目信息-分页列表查询", notes="项目信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CrmProject project,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                HttpServletRequest req) {
        log.debug("接收到查询参数: {}", project);
        log.debug("请求参数Map: {}", req.getParameterMap());
        
        // 处理日期范围查询参数
        String startDateBegin = req.getParameter("startDate_begin");
        if(startDateBegin != null && !startDateBegin.isEmpty()) {
            project.setStartDateBegin(startDateBegin);
            log.debug("添加开始日期起始条件: {}", startDateBegin);
        }
        
        String startDateEnd = req.getParameter("startDate_end");
        if(startDateEnd != null && !startDateEnd.isEmpty()) {
            project.setStartDateEnd(startDateEnd);
            log.debug("添加开始日期结束条件: {}", startDateEnd);
        }
        
        String endDateBegin = req.getParameter("endDate_begin");
        if(endDateBegin != null && !endDateBegin.isEmpty()) {
            project.setEndDateBegin(endDateBegin);
            log.debug("添加结束日期起始条件: {}", endDateBegin);
        }
        
        String endDateEnd = req.getParameter("endDate_end");
        if(endDateEnd != null && !endDateEnd.isEmpty()) {
            project.setEndDateEnd(endDateEnd);
            log.debug("添加结束日期结束条件: {}", endDateEnd);
        }
        
        String projectAmountBegin = req.getParameter("projectAmount_begin");
        if(projectAmountBegin != null && !projectAmountBegin.isEmpty()) {
            project.setProjectAmountBegin(projectAmountBegin);
            log.debug("添加项目金额起始条件: {}", projectAmountBegin);
        }
        
        String projectAmountEnd = req.getParameter("projectAmount_end");
        if(projectAmountEnd != null && !projectAmountEnd.isEmpty()) {
            project.setProjectAmountEnd(projectAmountEnd);
            log.debug("添加项目金额结束条件: {}", projectAmountEnd);
        }
        
        Page<CrmProject> page = new Page<CrmProject>(pageNo, pageSize);
        IPage<CrmProject> pageList = projectService.getProjectPageList(page, project);
        return Result.OK(pageList);
    }
    
    /**
     * 根据客户ID获取项目列表
     *
     * @param customerId
     * @return
     */
    @AutoLog(value = "项目信息-根据客户ID获取项目列表")
    @ApiOperation(value="项目信息-根据客户ID获取项目列表", notes="项目信息-根据客户ID获取项目列表")
    @GetMapping(value = "/getByCustomerId")
    public Result<?> getByCustomerId(@RequestParam(name="customerId", required=true) String customerId) {
        List<CrmProject> projectList = projectService.getProjectsByCustomerId(customerId);
        return Result.OK(projectList);
    }
    
    /**
     * 添加
     *
     * @param project
     * @return
     */
    @AutoLog(value = "项目信息-添加")
    @ApiOperation(value="项目信息-添加", notes="项目信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CrmProject project) {
        projectService.save(project);
        return Result.OK("添加成功！");
    }
    
    /**
     * 编辑
     *
     * @param project
     * @return
     */
    @AutoLog(value = "项目信息-编辑")
    @ApiOperation(value="项目信息-编辑", notes="项目信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmProject project) {
        projectService.updateById(project);
        return Result.OK("编辑成功!");
    }
    
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "项目信息-通过id删除")
    @ApiOperation(value="项目信息-通过id删除", notes="项目信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id", required=true) String id) {
        projectService.removeById(id);
        return Result.OK("删除成功!");
    }
    
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "项目信息-批量删除")
    @ApiOperation(value="项目信息-批量删除", notes="项目信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids) {
        this.projectService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
    
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "项目信息-通过id查询")
    @ApiOperation(value="项目信息-通过id查询", notes="项目信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id", required=true) String id) {
        CrmProject project = projectService.getById(id);
        if(project==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(project);
    }
    
    /**
     * 导出excel
     *
     * @param request
     * @param project
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CrmProject project) {
        return super.exportXls(request, project, CrmProject.class, "项目信息");
    }
    
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CrmProject.class);
    }
} 